﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using ClassModel;
using System.Data.SqlClient;

namespace ClassDAL
{
    public class t_class_FindPassword_DAL
    {
        /// <summary>
        /// 插入修改密码的token
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public static int InsertToken(t_class_findpassword_token token)
        {
            string insertSQL = "insert into t_class_findPassword_token values(@token,@createtime);";
            SqlParameter[] insertParams = new SqlParameter[] 
            {
                new SqlParameter("@token",token.token),
                new SqlParameter("@createtime",DateTime.Now)
            };

            return SQLHelper.ExecuteNonQuery(SQLHelper.GetConnection(), System.Data.CommandType.Text, insertSQL, insertParams);
        }

        /// <summary>
        ///  检查前token是否存在
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public static bool isTokenExists(string token)
        {
            string checkSQL = "select COUNT(id) from t_class_findPassword_token where token = @token;";
            SqlParameter tokenParam = new SqlParameter() 
            {
                ParameterName = "@token",
                Value = token
            };

            return Convert.ToInt32(SQLHelper.ExecuteScalar(SQLHelper.GetConnection(), System.Data.CommandType.Text, checkSQL, tokenParam)) > 0;
        }


        /// <summary>
        ///  删除无效的token
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public static bool deleteToken(string token)
        {
            string deleteSQL = "delete from t_class_findPassword_token where token = @token;";
            SqlParameter deleteParam = new SqlParameter() 
            {
                ParameterName = "@token",
                Value = token
            };

            return SQLHelper.ExecuteNonQuery(SQLHelper.GetConnection(), System.Data.CommandType.Text, deleteSQL, deleteParam) > 0;
        }
    }
}
